package mapViewer.screens;

import context.*;

import mapViewer.AccountRecordStore;
import mapViewer.MapNavigator;
import mapViewer.MapSheet;
import simpleMapService.Location;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

//******************************************************************
//*** CLASS: AddMapSheetScreen
//******************************************************************
public class AddMapSheetScreen extends Form implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG
  private static final Command okCommand=new Command("OK",Command.ITEM,1);
  private static final Command cancelCommand=new Command("Cancel",Command.ITEM,1);

  private TextField sheetID;

  // ****************************************************************
  // *** CONSTRUCTOR: MapScheetScreen
  // ****************************************************************
  public AddMapSheetScreen(String title)
  { super(title);
    sheetID=new TextField("Sheet ident","",64,TextField.ANY);
    append(sheetID);
  }  

  //******************************************************************
  //*** METHOD: show
  //******************************************************************
  public void show()
  { final Displayable prev=Context.setCurrent(this);
    // addCommand(exitCommand);
    addCommand(okCommand);
    addCommand(cancelCommand);
    setCommandListener(new CommandListener()
    { public void commandAction(Command c, Displayable s)
      { if(c==okCommand)
        { String id=sheetID.getString();
   	      Location center=MapNavigator.getCurrentMapLoader().getMapCenter();
   	      int scale=MapNavigator.getCurrentMapLoader().getScale();
          MapSheet.add(id,center,scale);
          if(prev instanceof MapSheetScreen)((MapSheetScreen)prev).update();
          Context.setCurrent(prev);
          try { AccountRecordStore.storeAll(); } catch(Exception e) {}
        }
        else if(c==cancelCommand) Context.setCurrent(prev);
      }
    });
  }
}
